﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;
using System.IO.Compression;
using System.Net;
using TvdbLib;
using TvdbLib.Cache;
using TvdbLib.Data;
using TvdbLib.Data.Banner;
using TvdbLib.Exceptions;
using System.Diagnostics;


namespace tvdbInterface
{
    public class tvdbInterface
    {
        /// <summary>
        /// TvDB API Key.
        /// </summary>
        /// <remarks>Key registered for XMM - DO NOT USE OTHERWISE!</remarks>
        const string TvDBAPIKey = "9DA3FD5E44833FAA";

        /// <summary>
        /// Search the TvDB-Database for a Show-Title.
        /// </summary>
        /// <param name="sCacheRootPath">(string) Path to TvDB-XML-Cache. Usually in "[Library-Path]\tvdb\cache\</param>
        /// <param name="sSeriesTitle">(string) Name of the Show - used as search pattern.</param>
        /// <param name="sLanguage">(string) Preferred Language's International Abbreviation (e.g. "GER" for "German", "ENU" for "English, US", etc.)</param>
        /// <returns>(List[SearchResult]) List of tvdbInterface.SearchResult-Objects to pass over to XMM.</returns>
        public static List<SearchResult> findSeriesByTitle(string sCacheRootPath, string sSeriesTitle, string sLanguage)
        {
            List<SearchResult> lListViewResults = new List<SearchResult>();
            //XMM.Settings oSettings = new XMM.Settings();
            ICacheProvider iCacheProvider = new XmlCacheProvider(sCacheRootPath);
            iCacheProvider.InitCache();
            TvdbHandler vTvdbHandler = new TvdbHandler(iCacheProvider, TvDBAPIKey);
            TvdbLanguage vLanguage = vTvdbHandler.Languages[3]; // Select GERMAN as language.
            List<TvdbSearchResult> lResults = vTvdbHandler.SearchSeries(sSeriesTitle, vLanguage);
            if (lResults != null && lResults.Count > 0)
            {
                foreach (TvdbSearchResult vResult in lResults)
                {
                    string sBannerURL = string.Empty;

                    if(vResult.Banner.IsThumbLoaded)
                        sBannerURL = sCacheRootPath + vResult.Id.ToString() + "\\" + vResult.Banner.ThumbPath.Replace('/', '\\'); 

                    SearchResult oSearchResult = new SearchResult(vResult.SeriesName, vResult.Id, vResult.Overview, sBannerURL);

                    lListViewResults.Add(oSearchResult);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Keine Ergebnisse für \"" + sSeriesTitle + "\"!", "Kein Ergebnisse", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
            }

            return lListViewResults;
        }

        /// <summary>
        /// Get all info for a specific episode.
        /// </summary>
        /// <param name="iTvDBShowID"></param>
        /// <param name="iSeason"></param>
        /// <param name="iEpisode"></param>
        public static TvdbEpisode getEpisodeInfo(string sCacheRootPath, int iTvDBShowID, int iSeason, int iEpisode)
        {
            ICacheProvider iCacheProvider = new XmlCacheProvider(sCacheRootPath);
            iCacheProvider.InitCache();
            TvdbHandler vTvdbHandler = new TvdbHandler(iCacheProvider, TvDBAPIKey);
            TvdbLanguage vLanguage = vTvdbHandler.Languages[3]; // Select GERMAN as language.
            TvdbSeries vSeries = vTvdbHandler.GetSeries(iTvDBShowID, vLanguage, true, false, false);
            foreach(TvdbEpisode vEpisode in vSeries.Episodes)
            {
                if (vEpisode.SeasonNumber == iSeason && vEpisode.EpisodeNumber == iEpisode)
                {
                    return vEpisode;
                }
            }

            return null;
        }

        public static TvdbSeries downloadShowInfo(string sCacheRootPath, int iTvDBID, string sLanguage)
        {
            ICacheProvider iCacheProvider = new XmlCacheProvider(sCacheRootPath);
            iCacheProvider.InitCache();
            TvdbHandler vTvdbHandler = new TvdbHandler(iCacheProvider, TvDBAPIKey);
            TvdbLanguage vLanguage = vTvdbHandler.Languages[3];

            TvdbSeries oSeries = vTvdbHandler.GetFullSeries(iTvDBID, vLanguage, true);

            return oSeries;
        }

        public static TvdbSeries downloadShortShowInfo(string sCacheRootPath, int iTvDBID, string sLanguage)
        {
            ICacheProvider iCacheProvider = new XmlCacheProvider(sCacheRootPath);
            iCacheProvider.InitCache();
            TvdbHandler vTvdbHandler = new TvdbHandler(iCacheProvider, TvDBAPIKey);
            TvdbLanguage vLanguage = vTvdbHandler.Languages[3];

            //TvdbSeries oSeries = vTvdbHandler.GetFullSeries(iTvDBID, vLanguage, true);
            TvdbSeries oSeries = vTvdbHandler.GetBasicSeries(iTvDBID, vLanguage, true);

            return oSeries;
        }
    }
}
